package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.addMemberDto;
import com.zzyl.nursing.mapper.MemberVisitMapper;
import com.zzyl.nursing.service.IMemberVisitService;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * TODO
 *
 * @Author XS
 * @Date 2025/10/22 22:20
 */
@Service
public class MemberVisitServiceImpl extends ServiceImpl<MemberVisitMapper, Reservation> implements IMemberVisitService {

    @Autowired
    private MemberVisitMapper memberVisitMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public int getCount() {
        Long userId = UserThreadLocal.getUserId();
        //Reservation reservation = memberVisitMapper.selectById(userId);
        SysUser user = sysUserMapper.selectUserById(userId);
        if (user!=null){
            String phone =user.getPhonenumber();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        Long count = this.lambdaQuery().eq(Reservation::getMobile, phone)
                .between(Reservation::getCreateTime, startTime, endTime)
                .count();
        return Math.toIntExact(count);
        }
        throw new BaseException("没有找到预约用户");
    }


    @Override
    public void addMember(addMemberDto dto) {
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        reservation.setStatus(0);
//        reservation.setCreateTime(DateTime.now());
        memberVisitMapper.insert(reservation);
    }
}
